package bupt.party.online.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class Pagination extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	
	public Integer pageNow = 1;
	public Integer pageSize = 20;
	public Integer totalResults;
	public Integer pageCount;
	
	//my conditions
	public String currentYear;
	public String studentNO;
	public String studentName;
	public Integer firstPa;
	public Integer secondPa;
	
	//conditions2
	
	public String  highLevelQuery;
	public String queryContent;
	
	
	public Integer getPageNow() {
		return pageNow;
	}
	public void setPageNow(Integer pageNow) {
		this.pageNow = pageNow;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public Integer getTotalResults() {
		return totalResults;
	}
	public void setTotalResults(Integer totalResults) {
		this.totalResults = totalResults;
	}
	public Integer getPageCount() {
		return pageCount;
	}
	public void setPageCount(Integer pageCount) {
		this.pageCount = pageCount;
	}
	
	public String getCurrentYear() {
		return currentYear;
	}
	public void setCurrentYear(String currentYear) {
		this.currentYear = currentYear;
	}
	public String getStudentNO() {
		return studentNO;
	}
	public void setStudentNO(String studentNO) {
		this.studentNO = studentNO;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	
	public Integer getFirstPa() {
		return firstPa;
	}
	public void setFirstPa(Integer firstPa) {
		this.firstPa = firstPa;
	}
	public Integer getSecondPa() {
		return secondPa;
	}
	public void setSecondPa(Integer secondPa) {
		this.secondPa = secondPa;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	public static String getPaginationCode(int pageSize,int resultsCount,int pageNow){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String serverPath = request.getRequestURI().replace(".action", "");
		String pageCode = "";
		int pageLen = 7;
		int pageCount = (resultsCount%pageSize == 0 )? resultsCount/pageSize : resultsCount/pageSize + 1;
		int init = 1,max_p=pageCount;
		if(pageNow <= 0) pageNow = 1;
		
		pageLen = (pageLen%2 == 0)? pageLen : pageLen+1; //页码个数
		int pageOffset = (pageLen -1)/2;//页码偏移个数
		
		pageCode += "<span class=\"pagination\">";
		pageCode +="<div class=\"pagin fr\">";
		
		if(pageNow != 1){
			pageCode +="<a href=\""+serverPath+ "?pageNow="+(pageNow-1)+"\"><<</a>";
			
			if(pageNow > pageLen/2 && pageCount > pageLen){
				pageCode += "<a href=\""+serverPath+ "?pageNow=1\">"+1+"</a>";
			}
			if(pageNow > (pageLen + 1)/2+1 && pageCount > pageLen + 1) pageCode += "<span class=\"text\">...</span>";
		}
		else{
			pageCode += "<a class=\"disabled\"><<</a>";
		}
		
		if(pageCount > pageLen){
			if(pageNow <= pageOffset){
				init = 1;
				max_p = pageLen;
			}else{
				if(pageNow + pageOffset >= pageCount + 1){
					init = pageCount - pageLen + 1;
				}else{
					init = pageNow - pageOffset;
					max_p = pageNow + pageOffset;
				}
			}
		}
		
		for(int i = init;i<=max_p;i++){
			
			if(i == pageNow){
				pageCode += "<span class=\"current\">" + i + "</span>"; 
			}
			else{
				pageCode += "<a href=\""+serverPath+ "?pageNow="+i+"\">" +i+"</a>";
			}
		}
		
		if(pageNow != pageCount){
			if(pageNow < pageCount - pageOffset - 1 && pageCount > pageLen + 1){
				pageCode += "<span class=\"text\">...</span>";
			}
			if(pageNow < pageCount - pageOffset && pageCount > pageLen){
				pageCode += "<a href=\""+serverPath+ "?pageNow=" + pageCount +"\">" + pageCount +"</a>";
			}
			
			pageCode += "<a href=\""+serverPath+ "?pageNow=" + (pageNow+1) + "\">>></a>"; 
		}else{
			pageCode += "<a class=\"disabled\">>></a>";
		}
		
		pageCode += "&nbsp;&nbsp;共&nbsp;<font color=\"red\">"+resultsCount+"</font>&nbsp;条记录&nbsp&nbsp;第"+pageNow+"页/共"+pageCount+"页</div>";
		pageCode += "</span>";
		return pageCode;
	}
	
   public static String getPaginationCodeByURL(int pageSize,int resultsCount,int pageNow, String url){
		
	    String temp = "?";
		String pageCode = "";
		int pageLen = 7;
		int pageCount = (resultsCount%pageSize == 0 )? resultsCount/pageSize : resultsCount/pageSize + 1;
		int init = 1,max_p=pageCount;
		if(pageNow <= 0) pageNow = 1;
		
		pageLen = (pageLen%2 == 0)? pageLen : pageLen+1; //页码个数
		int pageOffset = (pageLen -1)/2;//页码偏移个数
		
		pageCode += "<span class=\"pagination\">";
		pageCode +="<div class=\"pagin fr\">";
		
		if(url.contains("?")) {
			temp = "&";
		}
		
		if(pageNow != 1){
			pageCode +="<a href=\"/"+url+ temp + "pageNow="+(pageNow-1)+"\"><<</a>";
			
			if(pageNow > pageLen/2 && pageCount > pageLen){
				pageCode += "<a href=\"/"+ url + temp + "pageNow=1\">"+1+"</a>";
			}
			if(pageNow > (pageLen + 1)/2+1 && pageCount > pageLen + 1) pageCode += "<span class=\"text\">...</span>";
		}
		else{
			pageCode += "<a class=\"disabled\"><<</a>";
		}
		
		
		if(pageCount > pageLen){
			if(pageNow <= pageOffset){
				init = 1;
				max_p = pageLen;
			}else{
				if(pageNow + pageOffset >= pageCount + 1){
					init = pageCount - pageLen + 1;
				}else{
					init = pageNow - pageOffset;
					max_p = pageNow + pageOffset;
				}
			}
		}
		
		for(int i = init;i<=max_p;i++){
			
			if(i == pageNow){
				pageCode += "<span class=\"current\">" + i + "</span>"; 
			}
			else{
				pageCode += "<a href=\"/"+ url + temp + "pageNow="+i+"\">" +i+"</a>";
			}
		}
		
		if(pageNow != pageCount){
			if(pageNow < pageCount - pageOffset - 1 && pageCount > pageLen + 1){
				pageCode += "<span class=\"text\">...</span>";
			}
			if(pageNow < pageCount - pageOffset && pageCount > pageLen){
				pageCode += "<a href=\"/"+ url + temp + "pageNow=" + pageCount +"\">" + pageCount +"</a>";
			}
			
			pageCode += "<a href=\"/"+ url + temp + "pageNow=" + (pageNow+1) + "\">>></a>"; 
		}else{
			pageCode += "<a class=\"disabled\">>></a>";
		}
		
		pageCode += "&nbsp;&nbsp;共&nbsp;<font color=\"red\">"+resultsCount+"</font>&nbsp;条记录&nbsp&nbsp;第"+pageNow+"页/共"+pageCount+"页</div>";
		pageCode += "</span>";
		return pageCode;
	}
   
   public static String getPaginationCodeForTree(int pageSize,int resultsCount,int pageNow){
		
		String pageCode = "";
		int pageLen = 7;
		int pageCount = (resultsCount%pageSize == 0 )? resultsCount/pageSize : resultsCount/pageSize + 1;
		int init = 1,max_p=pageCount;
		if(pageNow <= 0) pageNow = 1;
		
		pageLen = (pageLen%2 == 0)? pageLen : pageLen+1; //页码个数
		int pageOffset = (pageLen -1)/2;//页码偏移个数
		
		pageCode += "<span class=\"pagination\">";
		pageCode +="<div class=\"pagin fr\">";
		
		if(pageNow != 1){
			pageCode +="<a href=\"#\" onClick='javascript:goToPage("+(pageNow-1)+")'><<</a>";
			
			if(pageNow > pageLen/2 && pageCount > pageLen){
				pageCode += "<a href=\"#\" onClick='javascript:goToPage("+1+")'>1</a>";
			}
			if(pageNow > (pageLen + 1)/2+1 && pageCount > pageLen + 1) pageCode += "<span class=\"text\">...</span>";
		}
		else{
			pageCode += "<a class=\"disabled\"><<</a>";
		}
		
		
		if(pageCount > pageLen){
			if(pageNow <= pageOffset){
				init = 1;
				max_p = pageLen;
			}else{
				if(pageNow + pageOffset >= pageCount + 1){
					init = pageCount - pageLen + 1;
				}else{
					init = pageNow - pageOffset;
					max_p = pageNow + pageOffset;
				}
			}
		}
		
		for(int i = init;i<=max_p;i++){
			
			if(i == pageNow){
				pageCode += "<span class=\"current\">" + i + "</span>"; 
			}
			else{
				pageCode += "<a href=\"#\" onClick='javascript:goToPage("+ i +")'>" +i+"</a>";
			}
		}
		
		if(pageNow != pageCount){
			if(pageNow < pageCount - pageOffset - 1 && pageCount > pageLen + 1){
				pageCode += "<span class=\"text\">...</span>";
			}
			if(pageNow < pageCount - pageOffset && pageCount > pageLen){
				pageCode += "<a href=\"#\" onClick='javascript:goToPage("+ pageCount +")'>" + pageCount +"</a>";
			}
			
			pageCode += "<a href=\"#\"  onClick='javascript:goToPage("+ (pageNow+1) +")'>>></a>"; 
		}else{
			pageCode += "<a class=\"disabled\">>></a>";
		}
		
		pageCode += "&nbsp;&nbsp;共&nbsp;<font color=\"red\">"+resultsCount+"</font>&nbsp;条记录&nbsp&nbsp;第"+pageNow+"页/共"+pageCount+"页</div>";
		pageCode += "</span>";
		return pageCode;
	}
   
   //by year
   public static String getPaginationCodeByYear(int pageSize,int resultsCount,int pageNow,String currentYear){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String serverPath = request.getRequestURI().replace(".action", "");
		String pageCode = "";
		int pageLen = 7;
		int pageCount = (resultsCount%pageSize == 0 )? resultsCount/pageSize : resultsCount/pageSize + 1;
		int init = 1,max_p=pageCount;
		if(pageNow <= 0) pageNow = 1;
		
		pageLen = (pageLen%2 == 0)? pageLen : pageLen+1; //页码个数
		int pageOffset = (pageLen -1)/2;//页码偏移个数
		
		pageCode += "<span class=\"pagination\">";
		pageCode +="<div class=\"pagin fr\">";
		
		if(pageNow != 1){
			pageCode +="<a href=\""+serverPath+ "?pageNow="+(pageNow-1)+"&currentYear="+currentYear+"\"><<</a>";
			
			if(pageNow > pageLen/2 && pageCount > pageLen){
				pageCode += "<a href=\""+serverPath+ "?pageNow=1&currentYear="+currentYear+ "\">"+1+"</a>";
			}
			if(pageNow > (pageLen + 1)/2+1 && pageCount > pageLen + 1) pageCode += "<span class=\"text\">...</span>";
		}
		else{
			pageCode += "<a class=\"disabled\"><<</a>";
		}
		
		if(pageCount > pageLen){
			if(pageNow <= pageOffset){
				init = 1;
				max_p = pageLen;
			}else{
				if(pageNow + pageOffset >= pageCount + 1){
					init = pageCount - pageLen + 1;
				}else{
					init = pageNow - pageOffset;
					max_p = pageNow + pageOffset;
				}
			}
		}
		
		for(int i = init;i<=max_p;i++){
			
			if(i == pageNow){
				pageCode += "<span class=\"current\">" + i + "</span>"; 
			}
			else{
				pageCode += "<a href=\""+serverPath+ "?pageNow="+i+"&currentYear="+currentYear+"\">" +i+"</a>";
			}
		}
		
		if(pageNow != pageCount){
			if(pageNow < pageCount - pageOffset - 1 && pageCount > pageLen + 1){
				pageCode += "<span class=\"text\">...</span>";
			}
			if(pageNow < pageCount - pageOffset && pageCount > pageLen){
				pageCode += "<a href=\""+serverPath+ "?pageNow=" + pageCount +"&currentYear="+currentYear+"\">" + pageCount +"</a>";
			}
			
			pageCode += "<a href=\""+serverPath+ "?pageNow=" + (pageNow+1) +"&currentYear="+currentYear+ "\">>></a>"; 
		}else{
			pageCode += "<a class=\"disabled\">>></a>";
		}
		
		pageCode += "&nbsp;&nbsp;共&nbsp;<font color=\"red\">"+resultsCount+"</font>&nbsp;条记录&nbsp&nbsp;第"+pageNow+"页/共"+pageCount+"页</div>";
		pageCode += "</span>";
		return pageCode;
	}
   
   
   //by conditions
   public static String getPaginationCodeByConditons(int pageSize,int resultsCount,int pageNow,String currentYear,
		   String studentName,String studentNO,Integer firstPa,Integer secondPa){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String serverPath = request.getRequestURI().replace(".action", "");
		String pageCode = "";
		int pageLen = 7;
		int pageCount = (resultsCount%pageSize == 0 )? resultsCount/pageSize : resultsCount/pageSize + 1;
		int init = 1,max_p=pageCount;
		if(pageNow <= 0) pageNow = 1;
		
		pageLen = (pageLen%2 == 0)? pageLen : pageLen+1; //页码个数
		int pageOffset = (pageLen -1)/2;//页码偏移个数
		
		pageCode += "<span class=\"pagination\">";
		pageCode +="<div class=\"pagin fr\">";
		
		if(pageNow != 1){
			pageCode +="<a href=\""+serverPath+ "?pageNow="+(pageNow-1)+"&currentYear="+currentYear+"&studentName="+studentName+"&studentNO="+studentNO+"&firstPa="+firstPa+"&secondPa="+secondPa+"\"><<</a>";
			
			if(pageNow > pageLen/2 && pageCount > pageLen){
				pageCode += "<a href=\""+serverPath+ "?pageNow=1&currentYear="+currentYear+"&studentName="+studentName+"&studentNO="+studentNO+"&firstPa="+firstPa+"&secondPa="+secondPa+"\">"+1+"</a>";
			}
			if(pageNow > (pageLen + 1)/2+1 && pageCount > pageLen + 1) pageCode += "<span class=\"text\">...</span>";
		}
		else{
			pageCode += "<a class=\"disabled\"><<</a>";
		}
		
		if(pageCount > pageLen){
			if(pageNow <= pageOffset){
				init = 1;
				max_p = pageLen;
			}else{
				if(pageNow + pageOffset >= pageCount + 1){
					init = pageCount - pageLen + 1;
				}else{
					init = pageNow - pageOffset;
					max_p = pageNow + pageOffset;
				}
			}
		}
		
		for(int i = init;i<=max_p;i++){
			
			if(i == pageNow){
				pageCode += "<span class=\"current\">" + i + "</span>"; 
			}
			else{
				pageCode += "<a href=\""+serverPath+ "?pageNow="+i+"&currentYear="+currentYear+"&studentName="+studentName+"&studentNO="+studentNO+"&firstPa="+firstPa+"&secondPa="+secondPa+"\">" +i+"</a>";
			}
		}
		
		if(pageNow != pageCount){
			if(pageNow < pageCount - pageOffset - 1 && pageCount > pageLen + 1){
				pageCode += "<span class=\"text\">...</span>";
			}
			if(pageNow < pageCount - pageOffset && pageCount > pageLen){
				pageCode += "<a href=\""+serverPath+ "?pageNow=" + pageCount+"&currentYear="+currentYear+"&studentName="+studentName+"&studentNO="+studentNO+"&firstPa="+firstPa+"&secondPa="+secondPa+"\">" + pageCount +"</a>";
			}
			
			pageCode += "<a href=\""+serverPath+ "?pageNow=" + (pageNow+1)+"&currentYear="+currentYear+"&studentName="+studentName+"&studentNO="+studentNO+"&firstPa="+firstPa+"&secondPa="+secondPa+"\">>></a>"; 
		}else{
			pageCode += "<a class=\"disabled\">>></a>";
		}
		
		pageCode += "&nbsp;&nbsp;共&nbsp;<font color=\"red\">"+resultsCount+"</font>&nbsp;条记录&nbsp&nbsp;第"+pageNow+"页/共"+pageCount+"页</div>";
		pageCode += "</span>";
		return pageCode;
	}
   
   //by conditions2
   public static String getPaginationCodeByConditons2(int pageSize,int resultsCount,int pageNow,
		   Integer highLevelQuery,String queryContent,Integer firstPa,Integer secondPa){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String serverPath = request.getRequestURI().replace(".action", "");
		String pageCode = "";
		int pageLen = 7;
		int pageCount = (resultsCount%pageSize == 0 )? resultsCount/pageSize : resultsCount/pageSize + 1;
		int init = 1,max_p=pageCount;
		if(pageNow <= 0) pageNow = 1;
		
		pageLen = (pageLen%2 == 0)? pageLen : pageLen+1; //页码个数
		int pageOffset = (pageLen -1)/2;//页码偏移个数
		
		pageCode += "<span class=\"pagination\">";
		pageCode +="<div class=\"pagin fr\">";
		
		if(pageNow != 1){
			pageCode +="<a href=\""+serverPath+ "?pageNow="+(pageNow-1)+"&highLevelQuery="+highLevelQuery+"&queryContent="+queryContent+"&firstPa="+firstPa+"&secondPa="+secondPa+"\"><<</a>";
			
			if(pageNow > pageLen/2 && pageCount > pageLen){
				pageCode += "<a href=\""+serverPath+ "?pageNow=1&highLevelQuery="+highLevelQuery+"&queryContent="+queryContent+"&firstPa="+firstPa+"&secondPa="+secondPa+"\">"+1+"</a>";
			}
			if(pageNow > (pageLen + 1)/2+1 && pageCount > pageLen + 1) pageCode += "<span class=\"text\">...</span>";
		}
		else{
			pageCode += "<a class=\"disabled\"><<</a>";
		}
		
		if(pageCount > pageLen){
			if(pageNow <= pageOffset){
				init = 1;
				max_p = pageLen;
			}else{
				if(pageNow + pageOffset >= pageCount + 1){
					init = pageCount - pageLen + 1;
				}else{
					init = pageNow - pageOffset;
					max_p = pageNow + pageOffset;
				}
			}
		}
		
		for(int i = init;i<=max_p;i++){
			
			if(i == pageNow){
				pageCode += "<span class=\"current\">" + i + "</span>"; 
			}
			else{
				pageCode += "<a href=\""+serverPath+ "?pageNow="+i+"&&highLevelQuery="+highLevelQuery+"&queryContent="+queryContent+"&firstPa="+firstPa+"&secondPa="+secondPa+"\">" +i+"</a>";
			}
		}
		
		if(pageNow != pageCount){
			if(pageNow < pageCount - pageOffset - 1 && pageCount > pageLen + 1){
				pageCode += "<span class=\"text\">...</span>";
			}
			if(pageNow < pageCount - pageOffset && pageCount > pageLen){
				pageCode += "<a href=\""+serverPath+ "?pageNow=" + pageCount+"&highLevelQuery="+highLevelQuery+"&queryContent="+queryContent+"&firstPa="+firstPa+"&secondPa="+secondPa+"\">" + pageCount +"</a>";
			}
			
			pageCode += "<a href=\""+serverPath+ "?pageNow=" + (pageNow+1)+"&highLevelQuery="+highLevelQuery+"&queryContent="+queryContent+"&firstPa="+firstPa+"&secondPa="+secondPa+"\">>></a>"; 
		}else{
			pageCode += "<a class=\"disabled\">>></a>";
		}
		
		pageCode += "&nbsp;&nbsp;共&nbsp;<font color=\"red\">"+resultsCount+"</font>&nbsp;条记录&nbsp&nbsp;第"+pageNow+"页/共"+pageCount+"页</div>";
		pageCode += "</span>";
		return pageCode;
	}
	
	
}
